<?php
/**
	----------------------------------------------
				深圳九企科技
				www.9qi.cn
	----------------------------------------------
	
	会员首页
	@author xiaoming <maixiaoming@9qi.cn>
	@version $Id$
*/
class IndexAction extends BaseAction
{
    /**
		定义空操作
	*/
	public function _empty()
	{
		
		$this->_pageNotFound();
	}
	
	/**
		会员登录首页，可查看广告
    */
    public function index()
    {

		//$this->_checkLogin();  //检查登录
		$User = M('User');
		$Video = M('Video');
		
		$userId = Session::get('u_id');
		$userInfo = $User->where("u_id=$userId")->find();
		
		//视频分类
		$catId = isset($_POST['video_cat']) ? intval($_POST['video_cat']) : 0;
		/* 为该会员展示相应的广告视频 */
		$this->_showVideos($userInfo, $catId);
		
		$this->display();
		
		//echo 'login！！！';
    }

	/**
		会员登录程序
	*/
	public function login()
	{
		if (!( Session::is_set('u_id') && (Session::get('u_id') > 0)) ){
			if ($_POST['submitted']) {

				$User = M('User');
				$userName = $_POST['username'];
				$password = SHA1($_POST['password']);

				$result = $User->where("user_name='$userName' AND password='$password'")->find();

				if($result) {
					Session::set('u_name',$result['user_name']);
					Session::set('u_id',$result['u_id']);
					$this->redirect('index');
				} else {
					$this->redirect('index/login', '', 3, '密码不正确');
				}
			} else {
				$this->assign('title','login');
				$this->display();
			}
		} else {
			$this->redirect('index');
		}
	}
	
	/**
		會員註冊
	*/
	public function register()
	{
		if(isset($_POST['submitted'])) {
			$User = D('User');
			if($User->create()) {
				$User->password = SHA1($User->password);
				$User->reg_time = time();
				if ($uid = $User->add()){
					//註冊成功，設置session
					Session::set('u_name',$User->user_name);
					Session::set('u_id',$uid);
					$this->redirect('index', '', 3, 'Registration success!');
				}
			} else {
				echo $User->getError();
				exit();
			}
		} else {
		
			$this->display();
		}
		
	}
	
	/**
		會員登出
	*/
	public function logout()
	{
		Session::destroy();
		$this->redirect('index');
	}
	
	/**
		为该会员展示合适的广告视频
	*/
	private function _showVideos($userInfo = array(), $catId = 0)
	{
		$User = M('User');
		$Video = M('Video');

		import("ORG.Util.Page"); // 导入分页类
		
		//$condition = "zip_code LIKE '%{$userInfo['u_zip_code']},%' AND is_show=1";
		$condition = '';
		if(!empty($catId)) $condition .= " cat_id=$catId" ;
		$count = $Video->where($condition)->count();
		//echo $count;die;
		$Page = new Page($count,6);
		$show = $Page->show(); // 分页显示输出
		
		$videos = $Video->where($condition)
						->limit($Page->firstRow.','.$Page->listRows)->select();
		
		if(!empty($videos)) {
			foreach($videos as &$video) {
				$video['url'] = U("Apps://view/index?id={$video['video_id']}");
			}
		}
		
		//输出分类列表
		$Video_cat = M('video_cat');
		$video_cats = $Video_cat->select(); 
		$this->assign('video_cats',$video_cats);
		$this->assign('catId',$catId);

		$this->assign('video_count',$count);
		$this->assign('videos',$videos);
		$this->assign('page',$show);
		
		
	}
	
	/**
		接收facebook用户授权是数据，通过ajax传递到此页面
	*/
	public function saveUserInfo()
	{
		$Facebook = M('user');
		
		$response = json_decode(stripcslashes($_POST['response']), true);
		$data['facebook_id']	= $response['id'];

		$facebookUser = $Facebook->where("facebook_id={$data['facebook_id']}")->find();
		$status = array();
		if($facebookUser) {
			//已经存在该用户,则设置该用户session
			$_SESSION['u_id'] = $facebookUser['u_id'];
			$_SESSION['u_name'] = $facebookUser['user_name'];
			$status['status'] = 'exists';
		} else {
			//该用户不存在，在数据库添加用户信息
			$data['user_name']		= isset($response['name']) ? $response['name'] : '';
			$data['first_name']		= isset($response['first_name']) ? $response['first_name'] : '';
			$data['middle_name']	= isset($response['middle_name']) ? $response['middle_name'] : '';
			$data['last_name']		= isset($response['last_name']) ? $response['last_name'] : '';
			$data['link']			= isset($response['link']) ? $response['link'] : '';
			$data['birthday']		= isset($response['birthday']) ? $response['birthday'] : '';
			$data['gender']			= isset($response['gender']) ? $response['gender'] : '';
			$data['locale']			= isset($response['locale']) ? $response['locale'] : '';
			$data['email']			= isset($response['email']) ? $response['email'] : '';
			$data['password']		= SHA1(uniqid(rand(), TRUE));
			$result = $Facebook->add($data);
			if($result) {
				$_SESSION['u_id'] = $result;
				$_SESSION['u_name'] = $data['user_name'];
				$status['status'] = 'success';
			} else {
				$status['status'] = 'false';
			}
		}

		//$_SESSION['response_json'] = json_encode($status);
		echo json_encode($status);
		
	}
	
	public function show(){
		var_dump($_SESSION);
		
	}
}
?>